package com.gylang.im.core.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 获取请求的头 session中的数据
 *
 * @author gylang,
 * date 2020/5/11,
 * @version 1.0
 */
public class HttpInfoUtils {

    private final HttpServletRequest request;

    public HttpInfoUtils() {
       request = request();
    }

    public String getHeader(String key) {

        HttpServletRequest request = getRequest();
        //获取请求头的用户id
        return request.getHeader(key);

    }


    public Object getAttribute(String key) {

        HttpServletRequest request = getRequest();
        //获取请求头的用户id
        return request.getAttribute(key);

    }

    private static HttpServletRequest request() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert attributes != null;
        return attributes.getRequest();


    }

    public void setAttributes(String key, Object value) {
        getRequest().setAttribute(key, value);
    }

    public HttpServletRequest getRequest() {
        return request;
    }
}
